/**
 * @file	halPeripheral.c
 * @author	chipsea
 * @brief	
 * @version	0.1
 * @date	2020-11-30
 * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
 * @note
 */

/*********************************************************************
 * INCLUDES
 */
#include "log.h"
#include "key.h"
#include "OSAL.h"
#include "gpio.h"
#include "clock.h"
#include "error.h"
#include "pwrmgr.h"
#include "rom_sym_def.h"
#include "global_config.h"
#include "halPeripheral.h"
#include "rtc.h"

uint8 Hal_TaskID;

#define  SOFT_RESET_KEY		P0
#define  SOFT_RTC_KEY		P1


/**
* @fn void gpioNegedgeHandler(GpioPin_t pin, GpioWkupType_t type)
* @brief GPIO wakeup handler function.
* @param pin: specifies the port bits.
* @param type: interrupt signal type, reference GpioWkupType_t
* @return NONE.
*/
void gpioNegedgeHandler(GpioPin_t pin, gpio_polarity_e type)
{
	HalGpioUnregister(pin);
	if(HalGpioGet(SOFT_RESET_KEY) == 0) {
		hal_pwrmgr_lock(MOD_GPIO);
		VOID osal_start_timerEx( Hal_TaskID, USR_TEST_TIMER1_EVT, 50);
		osal_set_event(Hal_TaskID,USR_RESET_EVT);
	}else if(HalGpioGet(SOFT_RTC_KEY) == 0) {
        UTCTime time;
		UTCTimeStruct rtcGetTime;
		hal_pwrmgr_lock(MOD_GPIO);
		VOID osal_start_timerEx( Hal_TaskID, USR_TEST_TIMER1_EVT, 50);
		HalRtcGetTime(&rtcGetTime); 		//this function should be called periodicly
		LOG("Current time: %d/%d/%d %d:%d:%d\r\n",rtcGetTime.year,rtcGetTime.month+1,rtcGetTime.day+1,rtcGetTime.hour,rtcGetTime.minutes,rtcGetTime.seconds);
        time = HalRtcGet_Utc();
        LOG("utc:%d\r\n", time);
	}
	HalGpioRegister(pin,NULL,gpioNegedgeHandler);
}


/**
* @fn void sample_gpioWakeupCheck(void)
* @brief GPIO wakeup check
* @param NONE
* @return NONE.
*/
void sample_gpioWakeupCheck(void)
{
	if(HalGpioGet(SOFT_RESET_KEY) == 0 || HalGpioGet(SOFT_RTC_KEY) == 0){
		hal_pwrmgr_lock(MOD_GPIO);
		VOID osal_start_timerEx( Hal_TaskID, USR_TEST_TIMER1_EVT, 50);
	}
	else{
		hal_pwrmgr_unlock(MOD_GPIO);
	}
}


/**
 * @fn      void HalPeripheral_Init(uint8 task_id)
 * @brief   Peripheral initialization
 * @param   task_id
 * @return  none
 */
void HalPeripheral_Init(uint8 task_id)
{
	Hal_TaskID = task_id;
	
	HalGpioPinInit(SOFT_RESET_KEY,GPIO_INPUT);
	HalGpioPupdConfig(SOFT_RESET_KEY, GPIO_PULL_UP);
	HalGpioWkupConfig(SOFT_RESET_KEY,POL_FALLING);
	HalGpioRegister(SOFT_RESET_KEY,NULL,gpioNegedgeHandler);

	HalGpioPinInit(SOFT_RTC_KEY,GPIO_INPUT);
	HalGpioPupdConfig(SOFT_RTC_KEY, GPIO_PULL_UP);
	HalGpioWkupConfig(SOFT_RTC_KEY,POL_FALLING);
	HalGpioRegister(SOFT_RTC_KEY,NULL,gpioNegedgeHandler);
	
	HalRtcInit();
	osal_start_timerEx( Hal_TaskID, USR_TEST_TIMER1_EVT,1000);
}

/**
 * @fn      uint16 HalPeripheral_ProcessEvent( uint8 task_id, uint16 events )
 * @brief   Peripheral event process
 * @param   task_id
 * @param	events
 * @return  none
 */
uint16 HalPeripheral_ProcessEvent( uint8 task_id, uint16 events )
{
	if(task_id != Hal_TaskID)
    {
		return 0;
	}
	
	if( events & USR_TEST_TIMER1_EVT )
    {
		sample_gpioWakeupCheck();
		return ( events ^ USR_TEST_TIMER1_EVT );
    }
	if(events & USR_RESET_EVT) {
		while(HalGpioGet(SOFT_RTC_KEY) == 0);
		LOG("Reset\r\n");
		NVIC_SystemReset();
	return ( events ^ USR_RESET_EVT );
	}
    
    return 0;
}

/*********************************************************************
*********************************************************************/
